//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.net.qsadmin.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.quickserver.swing.JFrameUtilities;
import org.quickserver.util.ClassUtil;
import org.quickserver.util.MyString;
import org.quickserver.util.io.DirFileList;
import org.quickserver.util.xmlreader.PluginConfigReader;
import org.quickserver.util.xmlreader.QSAdminPluginConfig;

public class QSAdminGUI extends JPanel {
    private static Logger logger = Logger.getLogger(QSAdminGUI.class.getName());
    private static QSAdminMain qsadminMain = null;
    private static String pluginDir = "./../plugin";
    private ClassLoader classLoader = this.getClass().getClassLoader();
    public ImageIcon logo;
    public ImageIcon logoAbout;
    public ImageIcon ball;
    private HeaderPanel headerPanel;
    private MainCommandPanel mainCommandPanel;
    private CmdConsole cmdConsole;
    private PropertiePanel propertiePanel;
    private JTabbedPane tabbedPane;
    private JFrame parentFrame;
    final HashMap pluginPanelMap;
    private ArrayList plugins;
    private JMenu mainMenu;
    private JMenu helpMenu;
    private JMenuBar jMenuBar;
    private JMenuItem loginMenuItem;
    private JMenuItem exitMenuItem;
    private JMenuItem aboutMenuItem;

    public void logComand(String command, char type) {
        logger.info("For[" + type + "] " + command);
    }

    public static void showGUI(String[] args, final SplashScreen splash) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");
                } catch (Exception var4) {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception var3) {
                        ;
                    }
                }

                QSAdminGUI.qsadminMain = new QSAdminMain();
                JFrame frame = new JFrame("QSAdmin GUI");
                frame.setDefaultCloseOperation(3);
                QSAdminGUI qsAdminGUI = new QSAdminGUI(QSAdminGUI.qsadminMain, frame);
                qsAdminGUI.updateConnectionStatus(false);
                frame.getContentPane().add(qsAdminGUI);
                frame.pack();
                frame.setSize(700, 450);
                frame.setIconImage(qsAdminGUI.logo.getImage());
                JFrameUtilities.centerWindow(frame);
                frame.setVisible(true);
                if (splash != null) {
                    splash.kill();
                }

            }
        });
    }

    public QSAdminGUI(QSAdminMain qsadminMain, JFrame parentFrame) {
        this.logo = new ImageIcon(this.classLoader.getResource("icons/logo.gif"));
        this.logoAbout = new ImageIcon(this.classLoader.getResource("icons/logo.png"));
        this.ball = new ImageIcon(this.classLoader.getResource("icons/ball.gif"));
        this.pluginPanelMap = new HashMap();
        this.plugins = new ArrayList();
        this.parentFrame = parentFrame;
        qsadminMain.setGUI(this);
        this.setLayout(new BorderLayout(5, 5));
        this.headerPanel = new HeaderPanel(qsadminMain, parentFrame);
        this.mainCommandPanel = new MainCommandPanel(qsadminMain);
        this.cmdConsole = new CmdConsole(qsadminMain);
        this.propertiePanel = new PropertiePanel(qsadminMain);
        if (this.headerPanel != null && this.mainCommandPanel != null && this.cmdConsole != null && this.propertiePanel != null) {
            this.headerPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
            this.add(this.headerPanel, "North");
            JScrollPane propertieScrollPane = new JScrollPane(this.propertiePanel);
            JSplitPane splitPane = new JSplitPane(1, true, this.mainCommandPanel, this.cmdConsole);
            splitPane.setOneTouchExpandable(false);
            splitPane.setDividerLocation(250);
            this.tabbedPane = new JTabbedPane(1);
            this.tabbedPane.addTab("Main", this.ball, splitPane, "Main Commands");
            this.tabbedPane.addTab("Get/Set", this.ball, propertieScrollPane, "Properties Panel");
            QSAdminPluginConfig qsAdminPluginConfig = null;
            PluginPanel pluginPanel = null;

            try {
                File xmlFile = null;
                ClassLoader classLoader = null;
                Class mainClass = null;
                File file = new File(pluginDir);
                File[] dirs = null;
                if (file.canRead()) {
                    dirs = file.listFiles(new DirFileList());
                }

                for(int i = 0; dirs != null && i < dirs.length; ++i) {
                    xmlFile = new File(dirs[i].getAbsolutePath() + File.separator + "plugin.xml");
                    if (xmlFile.canRead()) {
                        qsAdminPluginConfig = PluginConfigReader.read(xmlFile);
                        if (qsAdminPluginConfig.getActive().equals("yes") && qsAdminPluginConfig.getType().equals("javax.swing.JPanel")) {
                            classLoader = ClassUtil.getClassLoaderFromJars(dirs[i].getAbsolutePath());
                            mainClass = classLoader.loadClass(qsAdminPluginConfig.getMainClass());
                            logger.fine("Got PluginMainClass " + mainClass);
                            pluginPanel = (PluginPanel)mainClass.newInstance();
                            if (JPanel.class.isInstance(pluginPanel)) {
                                logger.info("Loading plugin : " + qsAdminPluginConfig.getName());
                                this.pluginPanelMap.put("" + (2 + i), pluginPanel);
                                this.plugins.add(pluginPanel);
                                this.tabbedPane.addTab(qsAdminPluginConfig.getName(), this.ball, (JPanel)pluginPanel, qsAdminPluginConfig.getDesc());
                                pluginPanel.setQSAdminMain(qsadminMain);
                                pluginPanel.init();
                            }
                        } else {
                            logger.info("Plugin " + dirs[i] + " is disabled so skipping");
                        }
                    } else {
                        logger.info("No plugin configuration found in " + xmlFile + " so skipping");
                    }
                }
            } catch (Exception var14) {
                logger.warning("Error loading plugin : " + var14);
                logger.fine("StackTrace:\n" + MyString.getStackTrace(var14));
            }

            this.tabbedPane.addChangeListener(new ChangeListener() {
                int selected = -1;
                int oldSelected = -1;

                public void stateChanged(ChangeEvent e) {
                    this.selected = QSAdminGUI.this.tabbedPane.getSelectedIndex();
                    if (this.selected >= 2) {
                        ((PluginPanel)QSAdminGUI.this.pluginPanelMap.get("" + this.selected)).activated();
                    }

                    if (this.oldSelected >= 2) {
                        ((PluginPanel)QSAdminGUI.this.pluginPanelMap.get("" + this.oldSelected)).deactivated();
                    }

                    this.oldSelected = this.selected;
                }
            });
            this.add(this.tabbedPane, "Center");
            this.buildMenu();
        } else {
            throw new RuntimeException("Loading of one of gui component failed.");
        }
    }

    public void setStatus(String msg) {
        this.headerPanel.setStatus(msg);
    }

    public void setResponse(String res) {
        int msgType = -1;
        if (res.startsWith("+OK")) {
            msgType = 1;
        }

        if (res.startsWith("-ERR")) {
            msgType = 0;
        }

        JOptionPane.showMessageDialog(this, res.substring(res.indexOf(" ") + 1), "Response", msgType);
    }

    public void appendToConsole(String msg) {
        this.cmdConsole.append(msg);
    }

    public void setConsoleSend(boolean flag) {
        this.cmdConsole.setSendEdit(flag);
    }

    public void updateConnectionStatus(boolean connected) {
        if (connected) {
            this.headerPanel.setLogoutText();
            this.loginMenuItem.setText("Logout");
        } else {
            this.headerPanel.setLoginText();
            this.loginMenuItem.setText("Login...");
        }

        this.mainCommandPanel.updateConnectionStatus(connected);
        this.propertiePanel.updateConnectionStatus(connected);
        this.cmdConsole.updateConnectionStatus(connected);
        Iterator iterator = this.plugins.iterator();
        PluginPanel updatePluginPanel = null;

        while(iterator.hasNext()) {
            updatePluginPanel = (PluginPanel)iterator.next();
            updatePluginPanel.updateConnectionStatus(connected);
        }

        if (connected) {
            int selected = this.tabbedPane.getSelectedIndex();
            if (selected >= 2) {
                ((PluginPanel)this.pluginPanelMap.get("" + selected)).activated();
            }
        }

    }

    public static void setPluginDir(String dir) {
        pluginDir = dir;
    }

    public static String getPluginDir() {
        return pluginDir;
    }

    private void buildMenu() {
        this.jMenuBar = new JMenuBar();
        this.mainMenu = new JMenu();
        this.mainMenu.setText("Main");
        this.loginMenuItem = new JMenuItem("Login...");
        this.loginMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                QSAdminGUI.this.headerPanel.handleLoginLogout();
            }
        });
        this.mainMenu.add(this.loginMenuItem);
        this.exitMenuItem = new JMenuItem("Exit");
        this.exitMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (QSAdminGUI.qsadminMain.isConnected()) {
                    QSAdminGUI.this.headerPanel.handleLoginLogout();
                }

                System.exit(0);
            }
        });
        this.mainMenu.add(this.exitMenuItem);
        this.helpMenu = new JMenu();
        this.helpMenu.setText("Help");
        this.aboutMenuItem = new JMenuItem("About...");
        this.aboutMenuItem.setEnabled(true);
        this.aboutMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                QSAdminGUI.this.about();
            }
        });
        this.helpMenu.add(this.aboutMenuItem);
        this.jMenuBar.add(this.mainMenu);
        this.jMenuBar.add(this.helpMenu);
        this.parentFrame.setJMenuBar(this.jMenuBar);
    }

    private void about() {
        JOptionPane.showMessageDialog(this, "QSAdminGUI\n\nGUI Client for QSAdminServer of QuickServer.\nThis is compliant with QuickServer v" + QSAdminMain.VERSION_OF_SERVER + " release.\n\n" + "Copyright (C) QuickServer.org\n" + "http://www.quickserver.org", "About QSAdminGUI", 1, this.logoAbout);
    }
}
